import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Scanner;

public class Demo {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学号：");
        int id = scanner.nextInt();
        System.out.println("请输入姓名：");
        String name = scanner.next();
        //1.创建DataSource
        DataSource dataSource = new MysqlDataSource();
        //URL代表是网络上的位置(网址) - IP: 127.0.0.1, 端口号: 3306, bit: mysql中创建的库, utf8: 字符集, useSSL是否加密
        // ?后面代表的是 访问资源需要的一些参数
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/bit?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");//root:以管理员的身份运行
        ((MysqlDataSource)dataSource).setPassword("200456zjr");//密码

        //2.和数据库服务器建立连接
        Connection connection = dataSource.getConnection();

        //3. 构造 sql
        //String sql = "insert into student values(1,'张三')";
        //String sql = "delete from student where id = 1";
        //String sql = "update student set name = 'x' where id = 2";
        String sql = "insert into student values(?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,id);//替换第几个?
        statement.setString(2,name);
        System.out.println(statement);


        //4.把 sql 发给服务器
        //statement.executeQuery(); - select
        int n = statement.executeUpdate();//返回影响到的长度 - 其他
        System.out.println(n);

        //5.释放资源
        statement.close();
        connection.close();
    }
}
class Solution7 {
    public int maxAbsoluteSum(int[] nums) {
        int[] preSum = new int[nums.length+1];
        int max = 0; int min = 0;
        for(int i=0; i<nums.length; i++){
            preSum[i+1] = preSum[i]+nums[i];
            if(max < preSum[i+1]){
                max = preSum[i+1];
            }
            if(min > preSum[i+1]){
                min = preSum[i+1];
            }
        }
        return max-min;
    }
}
class Solution {
    public int wiggleMaxLength(int[] nums) {
        int prev = 0;
        int cur = 0;
        int count = 1;
        for(int i=0; i<nums.length-1; i++){
            cur = nums[i+1] - nums[i];
            if(prev>=0 && cur<0 || prev<=0 && cur>0){
                count++;
                prev = cur;
            }
        }
        return count;
    }
}